#include "front/front_interface.h"
using namespace ppspeech;

const wstring mcWstringPositiveQuantifiers =
	L"(点|朵|匹|张|座|回|场|尾|条|个|首|阙|阵|网|炮|顶|丘|棵|只|支|袭|辆|挑|"
	L"担|颗|壳|窠|曲|墙|群|腔|砣|座|客|贯|扎|捆|刀|令|打|手|罗|坡|山|岭|江|"
	L"溪|钟|队|单|双|对|口|头|脚|板|跳|枝|件|贴|针|线|管|名|位|身|堂|课|"
	L"本|页|家|户|层|丝|毫|厘|分|钱|两|担|铢|石|钧|锱|忽|"
	L"毫|厘|寸|尺|丈|寻|常|铺|程|撮|勺|注|"
	L"合|升|斗|石|盘|碗|碟|叠|桶|笼|盆|盒|杯|钟|斛|锅|簋|篮|盘|桶|罐|瓶|壶|"
	L"卮|盏|箩|箱|煲|啖|袋|钵|年|月|日|季|刻|时|周|天|秒|分|旬|纪|岁|世|"
	L"夜|代|伏|辈|丸|泡|粒|颗|幢|堆|条|根|支|道|面|片|张|颗|块|元|人|"
	L"折|室|球|轮|"
	L"摄氏度|华氏度|度|"
	L"公斤|斤|磅|"
	L"公里|里|"
	L"(公)分|分|"
	L"(千|分|厘|毫|微)米|米|"
	L"(千|毫|微)克|克|"
	L"(亿|千万|百万|万|千|百)|"
	L"(亿|千万|百万|万|千|百|美|)元|"
	L"(亿|千万|百万|万|千|百|)块|角|毛|分)";

const wregex mcWregexPositiveQuantifiers(L"(\\d+)([多余几])?" + mcWstringPositiveQuantifiers);


// 正整数 + 量词
void TextNormalizer::TnReZhQuantifiers(wstring* fpInWsentence, const LanguageEnum fcInLanguage)
{
	wsmatch tWsmatch;
	while (regex_search(*fpInWsentence, tWsmatch, mcWregexPositiveQuantifiers))
	{
		const wstring tcInWstr = (*fpInWsentence);
		const wstring tWstrMatch = tWsmatch[0];	//匹配到的字符串
		string tStrReplace;

		//----------------------------------------
		//分离数字与量词
		wstring tWstrNumber;
		for(const wchar_t& tcWchar : tWstrMatch) {
			if( IsWcharNumber(tcWchar) == false ) {
				break;
			}
			tWstrNumber += tcWchar;
		}
		wstring tWstrQuantifier = tWstrMatch.substr(tWstrNumber.length());

		//-----------------------------------------
		wstring tWstrAppendNegative = L"";	//追加前面的正负号
		wstring tWstrAppendDecimal = L"";	//追加前面的小数整数

		size_t tPosMatch = tWsmatch.position(0);
		size_t tLenMatch = tWsmatch.length(0);

		if((tPosMatch - 1 > 0)  &&  tcInWstr.at(tPosMatch-1) == L'.') {
			for(size_t i=2; i<tPosMatch; i++) {
				const wchar_t tcWchar = tcInWstr.at(tPosMatch-i);
				if(IsWcharNumber( tcWchar ) == false) {
					break;
				}

				tWstrAppendDecimal = tcWchar + tWstrAppendDecimal;
			}

			tPosMatch -= tWstrAppendDecimal.length() + 1;	//调整匹配位置
			tLenMatch += tWstrAppendDecimal.length() + 1;
		}

		if(  (tPosMatch - 1 > 0)  &&  (tcInWstr.at(tPosMatch-1)==L'-' || tcInWstr.at(tPosMatch-1)==L'+')  ) {
			tWstrAppendNegative = tcInWstr.at(tPosMatch-1);
			tPosMatch -= tWstrAppendNegative.length();		//调整匹配位置
			tLenMatch += tWstrAppendNegative.length();
		}

		//-----------------------------------------
		if(tWstrAppendDecimal != L"") {		//追加小数的整数部分
			tWstrNumber = tWstrAppendDecimal + L"." + tWstrNumber;
		}

		if(tWstrAppendNegative != L"") {	//追加正负号
			tWstrNumber = tWstrAppendNegative + tWstrNumber;
		}


		vector<wstring> tVecLastWords = GetWstrPositionLastWords(tcInWstr, tPosMatch-1, 1, LANG_ZH);
		wstring tWstrZh1Before = (tVecLastWords.size() > 0 ? tVecLastWords[0] : L"");		//前缀

		if(fcInLanguage==LANG_EN) {
			tStrReplace = ReplaceMethod(vector<string>{wstring2utf8string(tWstrNumber)}, RME_en_negative_decimal);
		}
		else {
			if(tWstrAppendDecimal != L"" || tWstrAppendNegative != L"") {	//有小数或正负号，2读成二
				tStrReplace = ReplaceMethod(vector<string>{wstring2utf8string(tWstrNumber)}, RME_zh_negative_decimal);	//小数负数
			}
			//正整数情况
			else if(tWstrQuantifier==L"折" || tWstrQuantifier==L"室") {	//85折、9折
				tStrReplace = ReplaceMethod(vector<string>{wstring2utf8string(tWstrNumber)}, RME_zh_digit_yi1);	//数字念
			}
			else if(tWstrZh1Before==L"第" && tWstrNumber==L"2") {
				tStrReplace = "二";
			}
			else if(tWstrZh1Before==L"第") {
				tStrReplace = ReplaceMethod(vector<string>{wstring2utf8string(tWstrNumber)}, RME_zh_integer_yi1_liang3);
			}
			else if( tWstrNumber==L"2" && (tWstrQuantifier==L"两" || tWstrQuantifier==L"队" || tWstrQuantifier==L"月") ) {		//"2两"读成"二两"
				tStrReplace = "二";
			}
			else {
				tStrReplace = ReplaceMethod(vector<string>{wstring2utf8string(tWstrNumber)}, RME_zh_integer_1yix_2liang3_yi1);	//取正整数部分
			}
		}

		tStrReplace += wstring2utf8string(tWstrQuantifier);		//补充量词单位

		WstrReplacePosition(fpInWsentence, tPosMatch, tLenMatch, utf8string2wstring(tStrReplace));
	}
}
